/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package riteservices.model.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import riteservices.model.bean.Fbfriends;
import riteservices.model.bean.Round;
import riteservices.model.bean.User;

/**
 *
 * @author sixviv
 */
public class RoundDAO extends GeneralDAO<Round>{

    public Round getRoundById(int id){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.setFetchMode("requests", FetchMode.JOIN);
        criteria.setFetchMode("users", FetchMode.JOIN);
        criteria.setFetchMode("user", FetchMode.JOIN);
        criteria.add(Restrictions.sqlRestriction("idRound="+id));
        Round round = (Round)criteria.uniqueResult();
        session.close();
        return round;
    }
    
    public List<Round> getRoundsByOwner(int idOwner){
        List<Round> result= new ArrayList<Round>();
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.add(Restrictions.sqlRestriction("User_idUser="+idOwner));        
        result = criteria.list();
        session.close();
        return result;
    }
    
    public List<Round> getPublicRoundsFriend(String destiny , String schedule, Fbfriends userFriend)
    {
        List<Round> result=new ArrayList<Round>();
        
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.setFetchMode("users", FetchMode.JOIN);
        Criteria criteriaRoute = criteria.createCriteria("route");
        criteria.add(Restrictions.eq("user.idUser", userFriend.getUserByUserIdUserF().getIdUser()));
        criteriaRoute.add(Restrictions.ilike("destiny", "%"+destiny+"%",MatchMode.ANYWHERE));
        criteria.add(Restrictions.ilike("schedule", "%"+schedule+"%",MatchMode.ANYWHERE));     
        result = criteria.list();
        session.close();        
        return result;
    }
    
    public List<Round> getPublicRounds(String destiny , String schedule, User user)
    {
        List<Round> result=new ArrayList<Round>();
        
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.setFetchMode("users", FetchMode.JOIN);
        Criteria criteriaRoute = criteria.createCriteria("route");
        criteria.add(Restrictions.ne("user.idUser", user.getIdUser()));
        criteriaRoute.add(Restrictions.ilike("destiny", "%"+destiny+"%",MatchMode.ANYWHERE));
        criteria.add(Restrictions.ilike("schedule", "%"+schedule+"%",MatchMode.ANYWHERE));     
        result = criteria.list();
        session.close();        
        return result;
    }
    
    public List<Round> getPublicRoundsFriendOrigin(String origin , String schedule, Fbfriends userFriend)
    {
        List<Round> result=new ArrayList<Round>();
        
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.setFetchMode("users", FetchMode.JOIN);
        Criteria criteriaRoute = criteria.createCriteria("route");
        criteria.add(Restrictions.eq("user.idUser", userFriend.getUserByUserIdUserF().getIdUser()));
        criteriaRoute.add(Restrictions.ilike("origin", "%"+origin+"%",MatchMode.ANYWHERE));
        criteria.add(Restrictions.ilike("schedule", "%"+schedule+"%",MatchMode.ANYWHERE));     
        result = criteria.list();
        session.close();        
        return result;
    }
    
    public List<Round> getPublicRoundsOrigin(String origin , String schedule, User user)
    {
        List<Round> result=new ArrayList<Round>();
        
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Round.class);
        criteria.setFetchMode("route", FetchMode.JOIN);
        criteria.setFetchMode("users", FetchMode.JOIN);
        Criteria criteriaRoute = criteria.createCriteria("route");
        criteria.add(Restrictions.ne("user.idUser", user.getIdUser()));
        criteriaRoute.add(Restrictions.ilike("origin", "%"+origin+"%",MatchMode.ANYWHERE));
        criteria.add(Restrictions.ilike("schedule", "%"+schedule+"%",MatchMode.ANYWHERE));     
        result = criteria.list();
        session.close();        
        return result;
    }

}
